from django.db import models

# Create your models here.
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericRelation,GenericForeignKey

class PythonBasic(models.Model):
    course_name = models.CharField(max_length=32)
    coupons = GenericRelation(to="Coupon")

class Oop(models.Model):
    course_name = models.CharField(max_length=32)
    coupons = GenericRelation(to="Coupon")


class Coupon(models.Model):
    coupon_name = models.CharField(max_length=32)
    content_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_obj = GenericForeignKey("content_type","object_id")

class User(models.Model):
    username = models.CharField("姓名",max_length=32)
    password = models.CharField("密码",max_length=32)
    user_type_entry = (
        (1,'Delux'),
        (2,'SVIP'),
        (3,'VVIP')
    )
    user_type = models.IntegerField(choices=user_type_entry)

class UserToken(models.Model):
    user = models.OneToOneField('User',on_delete=models.CASCADE)
    token = models.CharField(max_length=128)

class Author(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField("作者名",max_length=32)
    age = models.IntegerField(verbose_name="年龄")


    def __str__(self):
        return self.name


class Publish(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField("出版社名",max_length=32)
    city = models.CharField("城市",max_length=32)
    email = models.CharField("邮箱",max_length=32)

    def __str__(self):
        return self.name


class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField("书籍名称",max_length=32)
    price = models.DecimalField(max_digits=5,decimal_places=2,verbose_name="价格")
    publish = models.ForeignKey("Publish",verbose_name="出版社",related_name="book",related_query_name="query_book",on_delete=models.CASCADE)
    authors = models.ManyToManyField('Author',verbose_name="作者")

    def __str__(self):
        return self.title
